package servlet.register;

import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/UserServlet",name = "UserServlet")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String action = request.getParameter("action");
        if (action != null && action.length()>0) {
            switch (action) {
                case "active":
                    active(request,response);
                    break;
            }
        } else {
            response.getWriter().print("请勿乱搞！！！");
        }

    }

    private void active(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 拿参数
        String code = request.getParameter("code");
        String account = request.getParameter("account");

        boolean result = userService.active(account,code);

        if (result) {
            response.getWriter().print("验证通过！！！");
        } else {
            response.getWriter().print("验证失败！！！");
        }

    }


}
